package leetcode.day_2021_03_8_2021_03_14;

/**
 * 44. 通配符匹配
 * https://leetcode-cn.com/problems/wildcard-matching/
 */
public class LeetCode0044 {

    public static boolean isMatch(String s, String p) {
        int sLen = s.length();
        int pLen = p.length();
        boolean[][] dp = new boolean[sLen+1][pLen+1];
        dp[0][0] = true;
        for (int i=1; i<=pLen; i++) {
            dp[0][i] = dp[0][i-1] && p.charAt(i-1)=='*';
        }

        for (int i=1; i<=sLen; i++) {
            for (int j=1; j<=pLen; j++) {
                if (s.charAt(i-1)==p.charAt(j-1) || p.charAt(j-1)=='?') {
                    dp[i][j] = dp[i-1][j-1];
                } else if (p.charAt(j-1) == '*') {
                    dp[i][j] = dp[i][j-1] || dp[i-1][j];
                }
            }
        }
        return dp[sLen][pLen];
    }

    public static void main(String[] args) {
        System.out.println(isMatch("aa", "a"));
        System.out.println(isMatch("aa", "*"));
        System.out.println(isMatch("cb", "?a"));
        System.out.println(isMatch("adceb", "*a*b"));
        System.out.println(isMatch("acdcb", "a*c?b"));
    }
}
